/*
 * 修改用户密码模块
 * 2018年3月21日15:01:44
 * xwx
 */

(function() {

	var modpwd = {};
	var userid; //用户id
	var _oldpassword; //原密码
	var _newpassword; //新密码

	var userinfo = app.getUserInfo();
	userid = userinfo.id;
	_oldpassword = userinfo.password;

	mui('body').on('tap', '#btnModify', function() {

		if(!document.getElementById('fpassword').value) {
			mui.toast('请输入原密码');
			return;
		};
		_oldpassword = document.getElementById('fpassword').value;

		if(!document.getElementById('spassword').value) {
			mui.toast('请输入新密码');
			return;
		};

		_newpassword = document.getElementById('spassword').value;

		if(!document.getElementById('tpassword').value) {
			mui.toast('请再次输入新密码');
			return;
		};

		if(document.getElementById('spassword').value != _newpassword) {
			mui.toast('两次输入的密码不匹配');
			return;
		}

		modpwd = {
			id: userid,
			oldpassword: _oldpassword,
			newpassword: _newpassword
		}

		mui.confirm('确认修改密码', '温馨提示', '', function(e) {
			if(e.index == 1) {
				server.user.updateUserPwd(modpwd, function(r) {
					if(r.errCode == '0') {
						localStorage.removeItem('logininfo');
						localStorage.removeItem('userinfo');
						mui.toast('修改密码成功');
						setTimeout(function() {
							parentPage = top.mui.openWindow('login.html');
							mui.fire(parentPage, 'myfresh');
							return true;
						}, 500);

					} else {
						mui.toast(r.errMsg);
					}
				}, function(r) {
					mui.toast('服务器错误,请稍后刷新重试');
				});
			}
		}, 'div');
	});
})()